package com.ae.shield.client.gui;

import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.systems.RenderSystem;
import com.ae.shield.ModLib;
import com.ae.shield.client.gui.util.ButtonCustomized;
import com.ae.shield.client.gui.util.GuiHelper;
import com.ae.shield.common.gui.container.ShieldWorkbenchContainer;
import com.ae.shield.common.net.ModNetwork;
import com.ae.shield.common.net.toSever.ScreenButtonPressed;
import net.minecraft.client.gui.screen.inventory.ContainerScreen;
import net.minecraft.client.gui.widget.button.Button;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.text.TranslationTextComponent;

import javax.annotation.Nonnull;
import java.util.ArrayList;

public class ShieldWorkbenchScreen extends ContainerScreen<ShieldWorkbenchContainer> {
    private static final ResourceLocation GUI = new ResourceLocation(ModLib.MOD_ID, "textures/gui/shield_workbench.png");
    private Button in;
    private Button out;
    private final GuiHelper.FluidBar fluidBar;

    public ShieldWorkbenchScreen(ShieldWorkbenchContainer container, PlayerInventory playerInv, ITextComponent titleIn) {
        super(container, playerInv, titleIn);
        guiLeft = 0;
        guiTop = 0;
        xSize = 176;
        ySize = 181;
        GuiHelper guiHelper = new GuiHelper(this, xSize, ySize);
        fluidBar = guiHelper.addFluidBar(container.getTank(), 0);
    }

    @Override
    protected void init() {
        int left = (width - xSize) / 2;
        int up = (height - ySize) / 2;
        out = new ButtonCustomized(left + 44, up + 50, 15, 10, 175, 14, GUI, (b) -> {
            ModNetwork.CHANNEL.sendToServer(new ScreenButtonPressed(container.getPos(), 0));
        });
        in = new ButtonCustomized(left + 116, up + 50, 15, 10, 189, 14, GUI, (b) -> {
            ModNetwork.CHANNEL.sendToServer(new ScreenButtonPressed(container.getPos(), 1));
        });
        addButton(in);
        addButton(out);
        super.init();
    }

    @Override
    protected void drawGuiContainerBackgroundLayer(@Nonnull MatrixStack matrixStack, float partialTicks, int x, int y) {
        //GUI背景
        RenderSystem.color4f(1f, 1f, 1f, 1f);
        getMinecraft().textureManager.bindTexture(GUI);
        blit(matrixStack, guiLeft, guiTop, 0, 0, xSize, ySize);
        //能量
        int i = (int) Math.floor(container.getEnergyScaled() * 58);
        blit(matrixStack, guiLeft + 165, guiTop + 81 - i, 176, 150 - i, 3, i);
        //流体
        int fluidScaled = (int) Math.floor(container.getFluidScaled() * 58);
        fluidBar.drawFluid(8, 80 - fluidScaled, 16, fluidScaled);
        getMinecraft().textureManager.bindTexture(GUI);
        blit(matrixStack, guiLeft + 8, guiTop + 23, 176, 34, 16, 58);
    }

    @Override
    public void render(MatrixStack matrixStack, int mouseX, int mouseY, float partialTicks) {
        renderBackground(matrixStack);
        super.render(matrixStack, mouseX, mouseY, partialTicks);
        renderHoveredTooltip(matrixStack, mouseX, mouseY);
        fluidBar.drawFluidTip(mouseX, mouseY, 7, 21, 18, 60);
        drawEnergy(matrixStack, mouseX, mouseY);

    }

    @Override
    protected void drawGuiContainerForegroundLayer(@Nonnull MatrixStack matrixStack, int x, int y) {
        font.func_243248_b(matrixStack, playerInventory.getDisplayName(), (float) playerInventoryTitleX, (float) playerInventoryTitleY + 17, 4210752);
        font.func_243248_b(matrixStack, container.getDisplayName(), 8f, 4.5f, 4210752);
    }

    private void drawEnergy(@Nonnull MatrixStack matrixStack, int mouseX, int mouseY) {
        if (163 + guiLeft < mouseX && mouseX < 169 + guiLeft && 21 + guiTop < mouseY && mouseY < 82 + guiTop) {
            ArrayList<ITextComponent> list = new ArrayList<>();
            list.add(new TranslationTextComponent("screen.ae.shield.assembly.energy").append(new StringTextComponent(container.getEnergy() + " RF")));
            func_243308_b(matrixStack, list, mouseX, mouseY);
        }
    }
}
